//
//  SettingsViewController.m
//  school
//
//  Created by ci123 on 15-3-19.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "SettingsViewController.h"
#import "CellStyleSettings.h"
#import "SDImageCache.h"
#import "MBProgressHUD.h"
#import "AboutUsViewController.h"

@interface SettingsViewController ()

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    if(!UDIsMessageAccepted){
         [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UDIsMessageAcceptedIdentify];
    }
    [self _initNav];
    
    self.settingsTableView.backgroundColor = UIColorMake(244, 244, 244);
    self.settingsTableView.delegate = self;
    self.settingsTableView.dataSource = self;
    
    [self _initData];
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    [self calculateData];
}

- (void)_initNav {
    
   // self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"系统设置";
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20 , 44, 44)];
    button.showsTouchWhenHighlighted = YES;
    button.backgroundColor = [UIColor clearColor];    
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
    [button setImage:[UIImage imageNamed:@"nav_btn_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backPage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
    self.navigationItem.backBarButtonItem=backButton;
}

- (void)_initData {

    NSArray *tempArr = @[[[NSMutableArray alloc] initWithArray:@[@{@"title":@"接收消息提醒",@"value":@"0"},@{@"title":@"清除缓存",@"value":@"0.0M"}]],
                         [[NSMutableArray alloc] initWithArray:@[                                                                 @{@"title":@"关于我们",@"value":@""}]]];
    _data = [[NSMutableArray alloc] initWithArray:tempArr];
}

- (void)calculateData {

    NSString *isMessageAccepted = @"0";
    if (UDIsMessageAccepted && [[NSString stringWithFormat:@"%@", UDIsMessageAccepted] isEqualToString:@"1"]) {
        isMessageAccepted = @"1";
    }
    
    CGFloat size = [[SDImageCache sharedImageCache] getSize] / 1024.0 / 1024.0;
    NSString *cacheString = size >= 1 ? [NSString stringWithFormat:@"%.2fM",size] : [NSString stringWithFormat:@"%.1fK",size * 1024];
    
    _data[0][0] = @{@"title":@"接收消息提醒",@"value":isMessageAccepted};
    _data[0][1] = @{@"title":@"清除缓存",@"value":cacheString};
    
    [_settingsTableView reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    static NSString *CellIdentifier = @"CellStyleSettings";
    
    CellStyleSettings *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibArr = [[NSBundle mainBundle]loadNibNamed:@"CellStyleSettings" owner:self options:nil];
        cell = (CellStyleSettings *)[nibArr objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryNone;
        [cell.toggleBtn addTarget:self action:@selector(switchButtonClick:) forControlEvents:UIControlEventValueChanged];
    }
    
    cell.textLabel.text = _data[section][row][@"title"];
    cell.info.text = _data[section][row][@"value"];
    if (section == 0 && row == 0) {
        cell.toggleBtn.hidden = NO;
        cell.info.hidden = YES;
        cell.toggleBtn.on = [_data[section][row][@"value"] boolValue];
    }else {
        cell.info.hidden = NO;
        cell.toggleBtn.hidden = YES;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    if (section == 0 && row == 1) {
        [self clearCache];
    }else if (section == 1 && row == 0) {
    
        AboutUsViewController *about = [[AboutUsViewController alloc] init];
        about.title = @"关于我们";
        [self.navigationController pushViewController:about animated:YES];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if ([self.settingsTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.settingsTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.settingsTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.settingsTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 12;
    }
    return CGFLOAT_MIN;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _data.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_data[section] count];
}

//设置Cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - MBHud

- (void)showHud:(NSString *)title isDim:(BOOL)isDim {
    
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.dimBackground = isDim;
    if(title.length > 0) {
        _hud.labelText = title;
    }
}

- (void)showHudComplete:(NSString *)title success:(BOOL)success {
    NSString *name = success ? @"success" : @"excla";
    _hud.dimBackground = NO;
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
    _hud.mode = MBProgressHUDModeCustomView;
    if(title.length > 0) {
        _hud.labelText = title;
    }
    [_hud hide:YES afterDelay:1];
}

- (void)ShowExclaHud:(NSString *)title{
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self showHudComplete:title success:NO];
}

#pragma mark - Action

- (void)backPage:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)clearCache {
    
    [self showHud:@"清除中" isDim:YES];
    [self performSelectorOnMainThread:@selector(clearSDImageCache)
                           withObject:nil
                        waitUntilDone:YES];
    [self showHudComplete:@"清除成功" success:YES];
    [self calculateData];
}

- (void)clearSDImageCache {

    [[SDImageCache sharedImageCache] clearDisk];
}

- (void)switchButtonClick:(UISwitch *)sender {

    if (sender.on) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UDIsMessageAcceptedIdentify];
    }else {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UDIsMessageAcceptedIdentify];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
